package com.infodms.dms.actions.sales.salesInfoManage;

import java.sql.ResultSet;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.GetCommonArea;
import com.infodms.dms.common.materialManager.MaterialGroupManagerDao;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.dao.orgmng.GetOrgIdsOrDealerIdsDAO;
import com.infodms.dms.dao.sales.customerInfoManage.SalesInfoChangeQueryDAO;
import com.infodms.dms.dao.sales.customerInfoManage.SalesReportDAO;
import com.infodms.dms.dao.sales.salesInfoManage.SalesInfoChangeCheckDAO;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TmFleetPO;
import com.infodms.dms.po.TtCustomerEditLogPO;
import com.infodms.dms.po.TtCustomerPO;
import com.infodms.dms.po.TtDealerActualSalesAuditPO;
import com.infodms.dms.po.TtDealerActualSalesPO;
import com.infodms.dms.po.TtFleetContractPO;
import com.infodms.dms.util.CommonUtils;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.ActionContextExtend;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

/**
 * @Title : SalesInfoChangeCheck.java
 * @Package: com.infodms.dms.actions.sales.salesInfoManage
 * @Description: 实销信息更改审核
 * @date : 2010-6-30
 * @version: V1.0
 */
public class SalesInfoChangeCheck extends BaseDao {
	public Logger logger = Logger.getLogger(SalesInfoChangeCheck.class);
	private ActionContext act = ActionContext.getContext();
	private static final SalesInfoChangeCheck dao = new SalesInfoChangeCheck();

	public static final SalesInfoChangeCheck getInstance() {
		return dao;
	}

	RequestWrapper request = act.getRequest();
	private final String salesInfoChangeCheckInit = "/jsp/sales/salesInfoManage/salesInfoChangeCheckInit.jsp";
	private final String toCheckSalesInfoChange = "/jsp/sales/salesInfoManage/toCheckSalesInfoChange.jsp";
	private final String salseInfoChangeDetail = "/jsp/sales/salesInfoManage/salseInfoChangeDetail.jsp";

	/**
	 * @Title : salesInfoChangeCheckInit
	 * @Description: 实销信息更改审核页面初始化
	 * @throws
	 * @LastUpdate :2010-6-30
	 */
	public void salesInfoChangeCheckInit() {
		AclUserBean logonUser = null;
		try {
			ActionContextExtend atx = (ActionContextExtend) ActionContext.getContext();
			String reqURL = atx.getRequest().getContextPath();
			if("/CVS-SALES".equals(reqURL.toUpperCase())){
				act.setOutData("returnValue", 1);
			}else{
				act.setOutData("returnValue", 2);
			}
			logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(logonUser.getPoseId().toString());
			act.setOutData("areaList", areaList);
			act.setForword(salesInfoChangeCheckInit);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE, "实销信息更改审核页面初始化");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	/**
	 * @Title : changeListQuery
	 * @Description: 查询可审核信息
	 * @throws
	 * @LastUpdate :2010-6-30
	 */
	public void changeListQuery() {
		AclUserBean logonUser = null;
		try {
			logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
			act.getSession().get(Constant.LOGON_USER);
			String customer_type = CommonUtils.checkNull(request.getParamValue("customer_type")); 	// 客户类型
			String customer_name = CommonUtils.checkNull(request.getParamValue("customer_name")); 	// 客户名称
			String vin = CommonUtils.checkNull(request.getParamValue("vin")); 						// VIN
			String customer_phone = CommonUtils.checkNull(request.getParamValue("customer_phone")); // 客户电话
			String fleet_name = CommonUtils.checkNull(request.getParamValue("fleet_name")); 		// 集团客户名称
			String materialCode = CommonUtils.checkNull(request.getParamValue("materialCode")); 	// 选择物料组
			String is_fleet = CommonUtils.checkNull(request.getParamValue("is_fleet")); 			// 是否集团客户
			String contract_no = CommonUtils.checkNull(request.getParamValue("contract_no")); 		// 集团客户合同
			String startDate = CommonUtils.checkNull(request.getParamValue("startDate")); 			// 上报日期:开始
			String endDate = CommonUtils.checkNull(request.getParamValue("endDate")); 				// 上报日期 ：截止
			String dealerCode = CommonUtils.checkNull(request.getParamValue("dealerCode")); 		// 经销商代码
			String areaId = CommonUtils.checkNull(request.getParamValue("areaId"));					// 业务范围
			String orgId = logonUser.getOrgId().toString();
			String parentOrgId = logonUser.getParentOrgId();
			String dutyType = logonUser.getDutyType();
			String dealerSql = GetOrgIdsOrDealerIdsDAO.getDealerIdsForOrder(
					orgId, parentOrgId, dutyType, "TMD");
			Map<String, String> map = new HashMap<String, String>();
			map.put("customer_type", customer_type);
			map.put("customer_name", customer_name);
			map.put("vin", vin);
			map.put("customer_phone", customer_phone);
			map.put("fleet_name", fleet_name);
			map.put("materialCode", materialCode);
			map.put("is_fleet", is_fleet);
			map.put("contract_no", contract_no);
			map.put("startDate", startDate);
			map.put("endDate", endDate);
			map.put("dealerCode", dealerCode);
			map.put("areaId", areaId);
			map.put("dealerSql", dealerSql);
			
			Integer curPage = request.getParamValue("curPage") != null ? Integer.parseInt(request.getParamValue("curPage")) : 1; // 处理当前页
			PageResult<Map<String, Object>> ps = SalesInfoChangeCheckDAO.salesInfoChangeQueryList(map, Constant.PAGE_SIZE, curPage);
			act.setOutData("ps", ps);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE, "查询可审核信息 ");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	/**
	 * @Title : toCheck
	 * @Description: 实销信息更改审核PRE
	 * @throws
	 * @LastUpdate :2010-6-30
	 */
	public void toCheck() {
		AclUserBean logonUser = null;
		try {
			logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
			act.getSession().get(Constant.LOGON_USER);

			String vehicleId = CommonUtils.checkNull(request.getParamValue("vehicleId"));
			String isFleet = CommonUtils.checkNull(request.getParamValue("isFleet")); 					// 是否是集团客户
			String ctm_edit_id = CommonUtils.checkNull(request.getParamValue("ctm_edit_id")); 			// "客户信息更改"id
			String log_id = CommonUtils.checkNull(request.getParamValue("log_id")); 					// "实销信息更改"id

			Map<String, Object> dlrInfo = SalesReportDAO.getSaleDLRInfo(vehicleId) ;
			act.setOutData("dlrInfo", dlrInfo);
			
			Map<String, Object> vehicleInfo = SalesReportDAO.getVehicleInfo(vehicleId); 				// 1、车辆资料
			act.setOutData("vehicleInfo", vehicleInfo);
			TtDealerActualSalesAuditPO auditPO = new TtDealerActualSalesAuditPO();
			auditPO.setLogId(Long.parseLong(log_id));
			List aList = dao.select(auditPO);
			if (null != aList && aList.size() > 0) {
				TtDealerActualSalesAuditPO salesAuditInfo = (TtDealerActualSalesAuditPO) aList.get(0); // 2、销售信息
				act.setOutData("salesAuditInfo", salesAuditInfo);
				/*
				 * 如果是集团客户
				 */
				if (null != isFleet && isFleet.equals(Constant.IF_TYPE_YES + "")) {
					Long fleetId = salesAuditInfo.getFleetId(); 						// 集团客户id
					Long contractId = salesAuditInfo.getContractId(); 					// 集团客户合同id
					TmFleetPO fleetPO = new TmFleetPO();
					fleetPO.setFleetId(fleetId);
					List fList = dao.select(fleetPO);
					if (null != fList && fList.size() > 0) {
						String fleetName = ((TmFleetPO) fList.get(0)).getFleetName();   // 集团客户名称
						act.setOutData("fleetName", fleetName);
					}
					TtFleetContractPO contractPO = new TtFleetContractPO();
					contractPO.setContractId(contractId);
					List conList = dao.select(contractPO);
					if (null != conList && conList.size() > 0) {
						String contractNo = ((TtFleetContractPO) conList.get(0)).getContractNo(); // 集团客户合同
						act.setOutData("contractNo", contractNo);
					}
				} 
				TtCustomerEditLogPO editLogPO = new TtCustomerEditLogPO();
				editLogPO.setCtmEditId(Long.parseLong(ctm_edit_id));
				List cList = dao.select(editLogPO);
				if (null != cList && cList.size() > 0) {
					TtCustomerEditLogPO cusInfo = (TtCustomerEditLogPO) cList.get(0);
					act.setOutData("cusInfo", cusInfo); // 3、客户信息
					List linkManList = SalesReportDAO.getLink_List(cusInfo.getCtmId()); // 4、其他联系人信息
					act.setOutData("linkManList", linkManList);
				}
			}
			act.setForword(toCheckSalesInfoChange);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE, "实销信息更改审核PRE");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}

	/**
	 * @Title : checkSubmitAction
	 * @Description: 审核提交
	 * @throws
	 * @LastUpdate :2010-6-30
	 */
	public void checkSubmitAction() {
		AclUserBean logonUser = null;
		try {
			logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
			act.getSession().get(Constant.LOGON_USER);

			String checkStatus = CommonUtils.checkNull(request.getParamValue("checkStatus"));
			String logId = CommonUtils.checkNull(request.getParamValue("logId"));
			String ctmEditId = CommonUtils.checkNull(request.getParamValue("ctmEditId"));
			String isFleet = CommonUtils.checkNull(request.getParamValue("isFleet"));
			String checkRamark = CommonUtils.checkNull(request.getParamValue("checkRamark"));

			if (null != checkStatus && !"".equals(checkStatus)) {

				TtDealerActualSalesAuditPO tempAuditPO = new TtDealerActualSalesAuditPO();
				tempAuditPO.setLogId(Long.parseLong(logId));
				List auditList = dao.select(tempAuditPO);
				if (null != auditList && auditList.size() > 0) {
					// 修改后的实销信息
					TtDealerActualSalesAuditPO valueAuditPO = (TtDealerActualSalesAuditPO) auditList.get(0);
					// 如果通过
					if (checkStatus.equals(Constant.SALES_INFO_CHANGE_STATUS_02 + "")) {
						Long orderId = valueAuditPO.getOrderId();
						TtDealerActualSalesPO tempSalesPO = new TtDealerActualSalesPO();
						tempSalesPO.setOrderId(orderId);
						List salesList = dao.select(tempSalesPO);
						if (null != salesList && salesList.size() > 0) {
							// 修改前的实销信息
							TtDealerActualSalesPO valueSalesPO = (TtDealerActualSalesPO) salesList.get(0);
							
							TtDealerActualSalesPO chnSalesPO = new TtDealerActualSalesPO();
							chnSalesPO.setInvoiceDate(valueAuditPO.getInvoiceDate());
							chnSalesPO.setInvoiceNo(valueAuditPO.getInvoiceNo() == null ? " " : valueAuditPO.getInvoiceNo());
							chnSalesPO.setContractNo(valueAuditPO.getContractNo() == null ? " " : valueAuditPO.getContractNo());
							chnSalesPO.setPrice(valueAuditPO.getPrice());
							chnSalesPO.setPayment(valueAuditPO.getPayment());
							chnSalesPO.setMiles(valueAuditPO.getMiles());
							chnSalesPO.setConsignationDate(valueAuditPO.getConsignationDate());
							chnSalesPO.setSalesAddress(valueAuditPO.getSalesAddress());
							chnSalesPO.setSalesReson(valueAuditPO.getSalesReson());
							chnSalesPO.setVehicleNo(valueAuditPO.getVehicleNo() == null ? " " : valueAuditPO.getVehicleNo());
							chnSalesPO.setInsuranceCompany(valueAuditPO.getInsuranceCompany() == null ? " " : valueAuditPO.getInsuranceCompany());
							Date insuranceDate = valueAuditPO.getInsuranceDate();
							if (null != insuranceDate && !"".equals(insuranceDate)) {
								chnSalesPO.setInsuranceDate(insuranceDate);
							}
							chnSalesPO.setMemo(valueAuditPO.getMemo() == null ? " " : valueAuditPO.getMemo());
							// 如果是集团客户
							if (null != isFleet && isFleet.equals(Constant.IF_TYPE_YES + "")) {
								chnSalesPO.setIsFleet(Constant.IF_TYPE_YES);
								chnSalesPO.setFleetId(valueAuditPO.getFleetId());
								chnSalesPO.setContractId(valueAuditPO.getContractId());
							}
							// 执行实销信息更改操作
							TtDealerActualSalesPO sPO = new TtDealerActualSalesPO();
							sPO.setOrderId(valueSalesPO.getOrderId());
							dao.update(sPO, chnSalesPO);
							// 修改实销更改申请表的审核状态
							TtDealerActualSalesAuditPO salPO = new TtDealerActualSalesAuditPO();
							salPO.setStatus(Constant.SALES_INFO_CHANGE_STATUS_02);
							if (null != checkRamark && !"".equals(checkRamark)) {
								salPO.setCheckRamark(checkRamark.trim());
							}
							
							TtDealerActualSalesAuditPO tActualSalesAuditPO = new TtDealerActualSalesAuditPO();
							tActualSalesAuditPO.setLogId(Long.parseLong(logId));
							dao.update(tActualSalesAuditPO, salPO);

							// 如果不是集团客户，修改客户信息
							if (null != isFleet && isFleet.equals(Constant.IF_TYPE_NO + "")) {
								TtCustomerEditLogPO tempCustomerEditLogPO = new TtCustomerEditLogPO();
								tempCustomerEditLogPO.setCtmEditId(Long.parseLong(ctmEditId));
								TtCustomerEditLogPO ttCustomerEditLogPO1=new TtCustomerEditLogPO();
								ttCustomerEditLogPO1.setIsReturn("0");
								dao.update(tempCustomerEditLogPO, ttCustomerEditLogPO1);
								List cusList = dao.select(tempCustomerEditLogPO);
								if (null != cusList && cusList.size() > 0) {
									// 修改后的客户信息
									TtCustomerEditLogPO valCustomerEditLogPO = (TtCustomerEditLogPO) cusList.get(0);
									Long ctmId = valCustomerEditLogPO.getCtmId();
									TtCustomerPO tempCustomerPO = new TtCustomerPO();
									tempCustomerPO.setCtmId(ctmId);
									
									List cuList = dao.select(tempCustomerPO);
									if (null != cuList && cuList.size() > 0) {
										// 修改前的客户信息
										TtCustomerPO valueCustomerPO = (TtCustomerPO) cuList.get(0);

										TtCustomerPO chnCustomerPO = new TtCustomerPO();
										chnCustomerPO.setCtmType(valCustomerEditLogPO.getCtmType());
										chnCustomerPO.setCardType(valCustomerEditLogPO.getCardType());
										chnCustomerPO.setCardNum(valCustomerEditLogPO.getCardNum() == null ? " " : valCustomerEditLogPO.getCardNum());
										chnCustomerPO.setSex(valCustomerEditLogPO.getSex());
										chnCustomerPO.setBirthday(valCustomerEditLogPO.getBirthday());
										chnCustomerPO.setIndustry(valCustomerEditLogPO.getIndustry());
										chnCustomerPO.setProfession(valCustomerEditLogPO.getProfession());
										chnCustomerPO.setJob(valCustomerEditLogPO.getJob() == null ? " " : valCustomerEditLogPO.getJob());
										chnCustomerPO.setCtmForm(valCustomerEditLogPO.getCtmForm());
										chnCustomerPO.setMainPhone(valCustomerEditLogPO.getMainPhone());
										chnCustomerPO.setOtherPhone(valCustomerEditLogPO.getOtherPhone());
										chnCustomerPO.setIncome(valCustomerEditLogPO.getIncome());
										chnCustomerPO.setEducation(valCustomerEditLogPO.getEducation());
										chnCustomerPO.setIsMarried(valCustomerEditLogPO.getIsMarried());
										chnCustomerPO.setEmail(valCustomerEditLogPO.getEmail() == null ? " " : valCustomerEditLogPO.getEmail());
										if (null != valCustomerEditLogPO.getProvince() && !"".equals(valCustomerEditLogPO.getProvince())) {
											chnCustomerPO.setProvince(valCustomerEditLogPO.getProvince());
										}
										if (null != valCustomerEditLogPO.getCity() && !"".equals(valCustomerEditLogPO.getCity())) {
											chnCustomerPO.setCity(valCustomerEditLogPO.getCity());
										}
										if (null != valCustomerEditLogPO.getTown() && !"".equals(valCustomerEditLogPO.getTown())) {
											chnCustomerPO.setTown(valCustomerEditLogPO.getTown());
										}
										chnCustomerPO.setAddress(valCustomerEditLogPO.getAddress());
										chnCustomerPO.setPostCode(valCustomerEditLogPO.getPostCode() == null ? " " : valCustomerEditLogPO.getPostCode());
										chnCustomerPO.setCompanyName(valCustomerEditLogPO.getCompanyName() == null ? " " : valCustomerEditLogPO.getCompanyName());
										chnCustomerPO.setCompanySName(valCustomerEditLogPO.getCompanySName() == null ? " " : valCustomerEditLogPO.getCompanySName());
										chnCustomerPO.setKind(valCustomerEditLogPO.getKind());
										chnCustomerPO.setLevelId(valCustomerEditLogPO.getLevelId());
										chnCustomerPO.setCompanyPhone(valCustomerEditLogPO.getCompanyPhone() == null ? " " : valCustomerEditLogPO.getCompanyPhone());
										if (null != valCustomerEditLogPO.getVehicleNum() && "0".equals(valCustomerEditLogPO.getVehicleNum() + "")) {
											chnCustomerPO.setVehicleNum(valCustomerEditLogPO.getVehicleNum());
										}
										TtCustomerPO cCustomerPO = new TtCustomerPO();
										cCustomerPO.setCtmId(ctmId);
										dao.update(cCustomerPO, chnCustomerPO);
									}
								}
							}
						}
					}
					// 如果驳回
					if (checkStatus.equals(Constant.SALES_INFO_CHANGE_STATUS_03 + "")) {
						TtCustomerEditLogPO tempCustomerEditLogPO = new TtCustomerEditLogPO();
						tempCustomerEditLogPO.setCtmEditId(Long.parseLong(ctmEditId));
						TtCustomerEditLogPO ttCustomerEditLogPO1=new TtCustomerEditLogPO();
						ttCustomerEditLogPO1.setIsReturn("0");
						dao.update(tempCustomerEditLogPO, ttCustomerEditLogPO1);
						// 修改实销更改申请表的审核状态
						TtDealerActualSalesAuditPO salPO = new TtDealerActualSalesAuditPO();
						salPO.setStatus(Constant.SALES_INFO_CHANGE_STATUS_03);
						if (null != checkRamark && !"".equals(checkRamark)) {
							salPO.setCheckRamark(checkRamark.trim());
						}
						TtDealerActualSalesAuditPO tAuditPO = new TtDealerActualSalesAuditPO();
						tAuditPO.setLogId(Long.parseLong(logId));
						dao.update(tAuditPO, salPO);
					}
				}
			}
			salesInfoChangeCheckInit();
		} catch (Exception e) {
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE, "实销信息更改审核:审核提交");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	/** 
	* @Title	  : checkDetailInfo 
	* @Description: 查看审核明细信息
	* @throws 
	* @LastUpdate :2010-7-1
	*/
	public void checkDetailInfo(){
		AclUserBean logonUser = null;
		try {
			logonUser = (AclUserBean) act.getSession().get(Constant.LOGON_USER);
			act.getSession().get(Constant.LOGON_USER);

			String vehicleId = CommonUtils.checkNull(request.getParamValue("vehicleId"));
			String isFleet = CommonUtils.checkNull(request.getParamValue("isFleet")); 					// 是否是集团客户
			String ctm_edit_id = CommonUtils.checkNull(request.getParamValue("ctm_edit_id")); 			// "客户信息更改"id
			String log_id = CommonUtils.checkNull(request.getParamValue("log_id")); 					// "实销信息更改"id

			Map<String, Object> vehicleInfo = SalesReportDAO.getVehicleInfo(vehicleId); 				// 1、车辆资料
			act.setOutData("vehicleInfo", vehicleInfo);
			TtDealerActualSalesAuditPO auditPO = new TtDealerActualSalesAuditPO();
			auditPO.setLogId(Long.parseLong(log_id));
			List aList = dao.select(auditPO);
			if (null != aList && aList.size() > 0) {
				TtDealerActualSalesAuditPO salesAuditInfo = (TtDealerActualSalesAuditPO) aList.get(0); // 2、销售信息
				act.setOutData("salesAuditInfo", salesAuditInfo);
				/*
				 * 如果是集团客户
				 */
				if (null != isFleet && isFleet.equals(Constant.IF_TYPE_YES + "")) {
					Long fleetId = salesAuditInfo.getFleetId(); 						// 集团客户id
					Long contractId = salesAuditInfo.getContractId(); 					// 集团客户合同id
					TmFleetPO fleetPO = new TmFleetPO();
					fleetPO.setFleetId(fleetId);
					List fList = dao.select(fleetPO);
					if (null != fList && fList.size() > 0) {
						String fleetName = ((TmFleetPO) fList.get(0)).getFleetName();   // 集团客户名称
						act.setOutData("fleetName", fleetName);
					}
					TtFleetContractPO contractPO = new TtFleetContractPO();
					contractPO.setContractId(contractId);
					List conList = dao.select(contractPO);
					if (null != conList && conList.size() > 0) {
						String contractNo = ((TtFleetContractPO) conList.get(0)).getContractNo(); // 集团客户合同
						act.setOutData("contractNo", contractNo);
					}
				}
				TtCustomerEditLogPO editLogPO = new TtCustomerEditLogPO();
				editLogPO.setCtmEditId(Long.parseLong(ctm_edit_id));
				List cList = dao.select(editLogPO);
				if (null != cList && cList.size() > 0) {
					TtCustomerEditLogPO cusInfo = (TtCustomerEditLogPO) cList.get(0);
					act.setOutData("cusInfo", cusInfo); // 3、客户信息

					List linkManList = SalesReportDAO.getLink_List(cusInfo.getCtmId()); // 4、其他联系人信息
					act.setOutData("linkManList", linkManList);
				}
			}
			act.setForword(salseInfoChangeDetail);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e, ErrorCodeConstant.QUERY_FAILURE_CODE, "查看审核明细信息");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
		
	}

	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO Auto-generated method stub
		return null;
	}
}
